﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DRHotel_v1.Classes;
using DRHotel_v1.Classes.DBManagers;

namespace DRHotel_v1.RoomAvaAndBooking
{
    public partial class RoomBookingFrm : Form
    {
        public Room rm;
        public Reservation res = new Reservation();
        public Customer cus = new Customer();
        public DateTime checkInDT;
        public DateTime checkOutDT;
        private bool isExistingCus = false;

        public RoomBookingFrm()
        {
            InitializeComponent();
        }

        private void RoomBookingFrm_Load(object sender, EventArgs e)
        {
            tbxRoomNo.Text = rm.roomID.ToString();
            tbxRoomRate.Text = rm.roomRates.ToString();
            tbxCapacity.Text = rm.capacity.ToString();
            tbxRoomType.Text = rm.roomType;
            tbxBedType.Text = rm.bedType;
            tbxSmoking.Text = rm.smoking.ToString();
            tbxCheckInDT.Text = checkInDT.ToShortDateString();
            tbxCheckOutDT.Text = checkOutDT.ToShortDateString();
            cbbAdult.SelectedIndex = 0;
            cbbChildren.SelectedIndex = 0;
        }

        #region Confirm booking button
        private void btnCfmBooking_Click(object sender, EventArgs e)
        {
            #region validation input
            if (tbxRoomNo.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Room No!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxRoomNo.SelectAll();
                tbxRoomNo.Focus();
                return;
            }
            if (cbbICType.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty IC Type!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cbbICType.SelectAll();
                return;
            }
            if (tbxICNo.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty IC Number!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (tbxFirstName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty First Name!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (tbxLastName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Last Name!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (cbbCustomerType.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Customer Type!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            
            if (tbxPhone.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Phone Number!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (tbxEmail.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Email Address!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if (!IsValidEmailAddress(tbxEmail.Text))
            {
                MessageBox.Show("Invalid Email Address!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxEmail.SelectAll();
                tbxEmail.Focus();
                return;
            }
            if (cbbAdult.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Number of Adults!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            #endregion

            #region fill and change classes info
            //fill Customer class
            cus.cusIC = tbxICNo.Text;
            cus.ICType = cbbICType.Text;
            cus.firstName = tbxFirstName.Text;
            cus.lastName = tbxLastName.Text;
            if (rbnFemale.Checked)
            {
                cus.gender = false;
            }
            else if (rbnMale.Checked)
            {
                cus.gender = true;
            }
            cus.cusType = cbbCustomerType.Text;
            DateTime dumy = new DateTime(9998, 12, 31);
            cus.dob = dumy;
            cus.country = "NULL";
            cus.mailingAddress = "NULL";
            cus.postCode = "NULL";
            cus.phoneNo = tbxPhone.Text;
            cus.email = tbxEmail.Text;
            

            //fill Reservation class
            res.checkOutDateTime = checkOutDT;
            res.checkInDateTime = checkInDT;
            res.roomID = Convert.ToInt32(tbxRoomNo.Text);
            res.cusIC = tbxICNo.Text;
            res.numOfAdult = Convert.ToInt32(cbbAdult.Text);
            res.numOfChildren = Convert.ToInt32(cbbChildren.Text);
            res.remark = tbxRemark.Text;
            res.resStatus = "Reserving";
            res.recordDateTime = DateTime.Now;

            #endregion

            #region Insert into DB
            //check if the customer is existing first, if existing insert a new customer, else update customer info
            //have to insert customer first because of DB dependency
            if (isExistingCus == false)
            {
                if (RoomAvaAndBookingDBManager.InsertCustomer(cus) == 0)
                {
                    MessageBox.Show("Customer Recorded Failed!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else if (RoomAvaAndBookingDBManager.UpdateRoomStatus(tbxRoomNo.Text, "Reserved") == 0)
                {
                    MessageBox.Show("Room Status Updated Failed!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else if (RoomAvaAndBookingDBManager.InsertReservation(res) == 0)
                {
                    MessageBox.Show("Reservation Created Failed!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    MessageBox.Show("Room Booked Sucessful!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                    //============================>>>display reservation record form
                }
            }
            else
            {
                cus.cusIC = tbxICNo.Text;
                if (RoomAvaAndBookingDBManager.UpdateCustomerInfo(cus) == 0)
                {
                    MessageBox.Show("Customer Recorded Failed!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else if (RoomAvaAndBookingDBManager.UpdateRoomStatus(tbxRoomNo.Text, "Reserved") == 0)
                {
                    MessageBox.Show("Room Status Updated Failed!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else if (RoomAvaAndBookingDBManager.InsertReservation(res) == 0)
                {
                    MessageBox.Show("Reservation Created Failed!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    MessageBox.Show("Room Booked Sucessful!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                    //============================>>>display reservation record form
                }
            }
            


            #endregion
        }

        public bool IsValidEmailAddress(string email)//validate email address
        {
            try
            {
                MailAddress ma = new MailAddress(email);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region Check customer IC button
        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (RoomAvaAndBookingDBManager.SearchExistingCus(tbxICNo.Text) == true)
            {
                MessageBox.Show("This customer is existing!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cus = RoomAvaAndBookingDBManager.GetCusByIC(tbxICNo.Text);
                cbbICType.Text = cus.ICType;
                tbxFirstName.Text = cus.firstName;
                tbxLastName.Text = cus.lastName;
                if (cus.gender == true)
                {
                    rbnMale.Checked = true;
                }
                else
                {
                    rbnFemale.Checked = true;
                }
                cbbCustomerType.Text = cus.cusType;
                tbxPhone.Text = cus.phoneNo;
                tbxEmail.Text = cus.email;
                isExistingCus = true;
                return;
            }
            else
            {
                btnCheck.Text = "Proceed";
                btnCheck.ForeColor = Color.White;
                btnCheck.BackColor = Color.Green;
            }
        }
        #endregion
    }
}
